import axios from 'axios'
import util from '@/util/util.js'
const methods = {
    //ajax请求函数
    delQuestion (index) {
        var id = this.question[index].id;
        this.$confirm('确认删除？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            this.$store.commit('showLoading');
            axios.get('/index/Question/delQuestion', {
                params: {
                    id: id
                }
            }).then((datas) => {
                
                var data = datas.data;
                if (data.status === 0) {
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    })
                }
                else {
                    
                }
                this.getQuestion();
               
            }, (err) => {
                this.getQuestion();
                
                console.log(err);
            })
        }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消删除'
            })
            
        });
    },

    updateQuestion (form) {
        this.$store.commit('showLoading');
        axios.get('/index/Question/editQuestion', {
            params: {
                id: form.id,
                kindName: form.kindName,
                payPrice: form.payPrice,
                questionContent: form.questionContent,
                isAnonymous: form.isAnonymous ? 1 : 0,
                isSolved: form.isSolved ? 1 : 0,
                isShowPage: form.isShowPage ? 1 : 0,
                isShow: form.isShow ? 1 : 0,
                isClose: form.isClose ? 1 : 0,
                isPublic: form.isPublic ? 1 : 0
            }
        }).then ((datas) => {
            var data = datas.data;
            if (data.status === 0) {
                this.$message({
                    message: '更新成功',
                    type: 'success'
                });
                
            }
            this.getQuestion();
        }, (err) => {   
            console.log(err)
            this.$message.error('更新失败，服务器挂了！');
            this.getQuestion();
        })
    },

    getQuestion () {
        var _this =  this;
        return new Promise(function (resolve, reject) {
            _this.$store.commit('showLoading');
            axios.get('/index/Question/getQuestion', {
            params: {
                keyName: _this.keyName,
                keyWord: _this.keyWord,
                page: _this.pageIndex,
                dataNum: _this.onePageDataNum
            }
        }).then((datas) => {
            var data = datas.data;
            
            if (data.status === 0) {
                    _this.question = _this.dealData(data.data.allNote);
                    _this.dataTotal = data.data.dataTotal;
                    _this.$store.commit('hiddenLoading');
            }
            else {

            }
            resolve();
            
        }, (err) => {
            console.log(err)
            reject();
        })
        })
    },
    //数据处理函数
    dealData (allData) {
        allData.forEach((val, index) => {
           allData[index].kindName = val.kindName === 'expert' ? '专家专问' : val.kindName;  
        });
        return allData;
    },
    //监听路由
    listenRoute () {
        var route = this.$route;
    
        var name = route.name;
        var query = route.query;
        var kindName = query.kindName ? query.kindName : '';
        var kindWord = query.kindWord ? query.kindWord : '';
        var page = query.page ? query.page : 1;
        
        this.kindName = kindName;
        this.kindWord = kindWord;
        this.pageIndex = page;
        
        this.nowPage = page * 1;

        this.getQuestion();
        
    },

    //行为触发函数
    
    refresh() {
        
        this.listenRoute();
    },
    searchQuestion (param) {
        var keyName = param.searchKind;
        var keyWord = util.getQuestionKey(param.searchKeyWord);
        this.keyName = keyName;
        this.keyWord = keyWord;

        var params = this.$route.query;
        params.keyName = keyName;
        params.keyWord = keyWord;
        this.$router.push({
            path: 'question',
            query: params
        });
        this.listenRoute()
    },
    choosePage (index) {
        //bug
        
        var route = this.$route;
        var params = route.query;
        params.page = index;
        
        this.$router.push({
            path: 'question',
            query: params
        })
        this.listenRoute()
    },

    lookQuestion (index) {
        var question = this.question[index];
        this.$refs.dialog.show(question);
    }
}
export default methods